// 工具类
const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : `0${n}`
}

module.exports = {
	formatTime,
	//跳转页面
	returnBack: (url, title, icon) => {
		wx.reLaunch({
			url: url,
			success: () => {
				wx.showToast({
					title: title,
					icon: icon,
					duration: 2000
				})
			}
		})
	},

	/**
	 * 计算两个日期时间相差的年数、月数、天数、小时数、分钟数、秒数
	 * DIFFTIME(开始时间,结束时间,[单位])，单位可以是 "y" 、"M"、"d"、"h"、"m"、"s"'
	 * console.log(DIFFTIME('2019-6-30 13:20:00', '2020-10-01 11:20:32', 's'))
	 */
	DIFFTIME: (startTime, endTime, unit) => {
		// 判断当前月天数
		function getDays(mouth, year) {
			let days = 30
			if (mouth === 2) {
				days = year % 4 === 0 ? 29 : 28
			} else if (mouth === 1 || mouth === 3 || mouth === 5 || mouth === 7 || mouth === 8 || mouth === 10 || mouth === 12) {
				// 月份为：1,3,5,7,8,10,12 时，为大月.则天数为31；
				days = 31
			}
			return days
		}
		const start = new Date(startTime)
		const end = new Date(endTime)
		// 计算时间戳的差
		const diffValue = end - start
		// 获取年
		const startYear = start.getFullYear()
		const endYear = end.getFullYear()
		// 获取月
		const startMouth = start.getMonth() + 1
		const endMouth = end.getMonth() + 1
		// 获取日
		const startDay = start.getDate()
		const endDay = end.getDate()
		// 获取小时
		const startHours = start.getHours()
		const endHours = end.getHours()
		// 获取分
		const startMinutes = start.getMinutes()
		const endMinutes = end.getMinutes()
		// 获取秒
		const startSeconds = start.getSeconds()
		const endSeconds = end.getSeconds()
		if (unit === 'y' || unit === 'M') {
			// 相差年份月份
			const diffYear = endYear - startYear
			// 获取当前月天数
			const startDays = getDays(startMouth, startYear)
			const endDays = getDays(endMouth, endYear)
			const diffStartMouth = (startDays - (startDay + ((startHours * 60 + startMinutes + startSeconds / 60) / 60 / 24) - 1)) / startDays
			const diffEndMouth = (endDay + ((endHours * 60 + endMinutes + endSeconds / 60) / 60 / 24) - 1) / endDays
			const diffMouth = diffStartMouth + diffEndMouth + (12 - startMouth - 1) + endMouth + (diffYear - 1) * 12
			if (unit === 'y') {
				return Math.floor(diffMouth / 12 * 100) / 100
			} else {
				return diffMouth
			}
		} else if (unit === 'd') {
			const d = parseInt(diffValue / 1000 / 60 / 60 / 24)
			return d
		} else if (unit === 'h') {
			const h = parseInt(diffValue / 1000 / 60 / 60)
			return h
		} else if (unit === 'm') {
			const m = parseInt(diffValue / 1000 / 60)
			return m
		} else if (unit === 's') {
			const s = parseInt(diffValue / 1000)
			return s
		} else {
			console.log('请输入正确的单位')
		}
	},

	// 获取当前年月日 YYYY-mm-dd格式
	getNowFormatDate: () => {
		let date = new Date(),
			year = date.getFullYear(), //获取完整的年份(4位)
			month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
			strDate = date.getDate() // 获取当前日(1-31)
		if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
		if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0

		return `${year}-${month}-${strDate}`
	}
}